from contextlib import contextmanager

@contextmanager
def my_file(path, mode):
    f = open(path, mode)
    yield f
    f.close

with my_file("a.txt", "w") as f:
    f.write("hello world!")
